//
//  SoundHandler.m
//  
//
//  Created by Fred Havemeyer on 6/22/08.
//  Copyright 2008 __MyCompanyName__. All rights reserved.
//

#import "SoundHandler.h"


@implementation SoundHandler
- (id)init
{
	if ((self = [super init]) != nil)
	{
		idle = swinging = striking = hitting = somethingPlaying = NO;
		
		baseSoundFolderPath = @"";
		
		[self setDelegate:self];
		[self setVibrationEnabled:YES];
		
		NSLog(@"Init sound handler!");
	}
	return self;
}
- (void)dealloc
{	
	[turnOn release];
	[turnOff release];
	
	[idle0 release];
	[idle1 release];
	
	[swing0 release];
	[swing1 release];
	[swing2 release];
	
	[hit0 release];
	[hit1 release];
	[hit2 release];
	
	[strike0 release];
	[strike1 release];
	
	[super dealloc];
}
- (void)playSoundOfType:(int)type
{
	switch (type)
	{
		case IDLE:
			if ((somethingPlaying && idle) || swinging || striking || hitting)
				return;
			idle = YES;
			swinging = striking = hitting = NO;
			[self playItem:idle1];
			break;
		case SWING:
			if ((somethingPlaying && swinging) || striking || hitting)
				return;
			swinging = YES;
			idle = striking = hitting = NO;
			[self playRandomSwing];
			break;
		case STRIKE:
			if ((somethingPlaying && striking) || hitting)
				return;
			striking = YES;
			idle = swinging = hitting = NO;
			[self playRandomStrike];
			break;
		case HIT:
			if (somethingPlaying && hitting)
				return;
			hitting = YES;
			idle = swinging = striking = NO;
			[self playRandomHit];
			break;
		case TURN_ON:
			if (*turningOn)	
				return;
			idle = swinging = striking = hitting = NO;
			*turningOn = YES;
			[self playItem:turnOn];
			break;
		case TURN_OFF:
			idle = swinging = striking = hitting = *turningOn = NO;
			[self playItem:turnOff];
	}
	somethingPlaying = YES;
}
- (void)playItem:(AVItem *)item
{
	somethingPlaying = YES;
	[self setCurrentItem:item];
	[self setCurrentTime:0.0];
	[self play:&err];
	
	if (err != nil)
		NSLog(@"Couldn't play item!");
	
	NSLog(@"Playing item!");
}
- (void)itemFinishedPlaying:(id)fp8
{
	NSLog(@"Item finished playing!");
	somethingPlaying = NO;
	
	if (*turningOn)
		*turningOn = NO;
	
	idle = swinging = hitting = striking = NO;
}
- (void)setBaseFilePath:(NSString *)path
{
	[baseSoundFolderPath release];
	baseSoundFolderPath = [NSString stringWithString:path];
}
- (void)setTurnOnDelegate:(BOOL *)turnDelegate
{
	turningOn = turnDelegate;
}
- (void)playRandomSwing
{
	srand(time(NULL));
	
	switch (rand() % 3)
	{
		case 0:
			[self playItem:swing0];
			break;
		case 1:
			NSLog(@"swing1");
			[self playItem:swing1];
			break;
		//case 2:
		//	NSLog(@"swing2");
		//	[self playItem:swing2];
		//	break;
		default:
			[self playItem:swing0];
			break;
	}
}
- (void)playRandomHit
{
	srand(time(NULL));
	
	switch (rand() % 3)
	{
		case 0:
			[self playItem:hit0];
			break;
		case 1:
			NSLog(@"hit1");
			[self playItem:hit1];
			break;
		case 2:
			NSLog(@"hit2");
			[self playItem:hit2];
			break;
		default:
			[self playItem:hit0];
			break;
	}
}
- (void)playRandomStrike
{
	srand(time(NULL));
	
	switch (rand() % 2)
	{
		case 0:
			[self playItem:strike0];
			break;
		case 1:
			NSLog(@"strike1");
			[self playItem:strike1];
			break;
		default:
			[self playItem:strike0];
			break;
	}
}
- (void)loadAllSoundsInBaseDir
{
	NSLog(@"Loading all sounds!");
	
	turnOn = [[AVItem alloc] initWithPath:[NSString stringWithFormat:@"%@on.wav", baseSoundFolderPath] error:&err];
	turnOff = [[AVItem alloc] initWithPath:[NSString stringWithFormat:@"%@off.wav", baseSoundFolderPath] error:&err];
	
	idle0 = [[AVItem alloc] initWithPath:[NSString stringWithFormat:@"%@idle0.wav", baseSoundFolderPath] error:&err];
	idle1 = [[AVItem alloc] initWithPath:[NSString stringWithFormat:@"%@idle1.wav", baseSoundFolderPath] error:&err];
	
	swing0 = [[AVItem alloc] initWithPath:[NSString stringWithFormat:@"%@swing0.wav", baseSoundFolderPath] error:&err];
	swing1 = [[AVItem alloc] initWithPath:[NSString stringWithFormat:@"%@swing1.wav", baseSoundFolderPath] error:&err];
	swing2 = [[AVItem alloc] initWithPath:[NSString stringWithFormat:@"%@swing2.wav", baseSoundFolderPath] error:&err];
	
	hit0 = [[AVItem alloc] initWithPath:[NSString stringWithFormat:@"%@hit0.wav", baseSoundFolderPath] error:&err];
	hit1 = [[AVItem alloc] initWithPath:[NSString stringWithFormat:@"%@hit1.wav", baseSoundFolderPath] error:&err];
	hit2 = [[AVItem alloc] initWithPath:[NSString stringWithFormat:@"%@hit2.wav", baseSoundFolderPath] error:&err];
	
	strike0 = [[AVItem alloc] initWithPath:[NSString stringWithFormat:@"%@strike0.wav", baseSoundFolderPath] error:&err];
	strike1 = [[AVItem alloc] initWithPath:[NSString stringWithFormat:@"%@strike1.wav", baseSoundFolderPath] error:&err];
	
	NSLog(@"Loading done!");
}
@end
